using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;

namespace Silverlight.Weblog.Client.DAL.Commands
{
    public class TextBoxEnterKeyPressedCommandBehavior : CommandBehaviorBase<TextBox>
    {
        public TextBoxEnterKeyPressedCommandBehavior(TextBox targetObject) : base(targetObject)
        {
            targetObject.KeyUp +=new KeyEventHandler(targetObject_KeyUp);
        }

        void  targetObject_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                TextBox tbx = (TextBox) sender;
                if (tbx != null)
                {
                    var textBinding = tbx.GetBindingExpression(TextBox.TextProperty);
                    if (textBinding != null)
                    {
                        textBinding.UpdateSource();
                    }
                }


                ExecuteCommand();
            }
        }
    }
}